package com.yuanmei.charts;


import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.View;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;

/**
 * http://stackoverflow.com/questions/28521004/mpandroidchart-have-one-graph-mirror-the-zoom-swipes-on-a-sister-graph
 */
public class GroupGestureListener implements OnChartGestureListener {

    private static final String TAG = "SyncGesture";

    private TaCombinedChart mSource;
    private TaCombinedChart[] mTargets;
    private OnDoubleTappedListener mDoubleTapListener;

    public interface OnDoubleTappedListener {
        void onDoubleTapped(int dataIndex);
    }

    public GroupGestureListener(TaCombinedChart owner,
                                TaCombinedChart[] targets,
                                OnDoubleTappedListener listener) {
        this.mSource = owner;
        this.mTargets = targets;
        this.mDoubleTapListener = listener;
    }

    @Override
    public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
    }

    @Override
    public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
    }

    @Override
    public void onChartLongPressed(MotionEvent me) {
    }

    @Override
    public void onChartDoubleTapped(MotionEvent me) {
        Highlight h = mSource.getHighlightByTouchPoint(me.getX(), me.getY());
//        Log.d(TAG, "onChartDoubleTapped getDataIndex=" + h.getDataIndex()
//                + " h: x=" + h.getX()
//                + ", y=" + h.getY()
//        );
        if (mDoubleTapListener != null) {
            mDoubleTapListener.onDoubleTapped((int)h.getX());
        }
    }

    @Override
    public void onChartSingleTapped(MotionEvent me) {
    }

    @Override
    public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
    }

    @Override
    public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
//        Log.d(TAG, "onChartScale " + scaleX + "/" + scaleY + " X=" + me.getX() + "Y=" + me.getY());
        syncTargetCharts();
    }

    @Override
    public void onChartTranslate(MotionEvent me, float dX, float dY) {
//        Log.d(TAG, "onChartTranslate " + dX + "/" + dY + " X=" + me.getX() + "Y=" + me.getY()
//                        + " Visual[Xmin=" + mSource.getLowestVisibleX()
//                        + ", Xmax=" + mSource.getHighestVisibleX() + "]"
//        );
        syncTargetCharts();
    }

    public void syncTargetCharts() {
        Matrix srcMatrix;
        float[] srcVals = new float[9];
        Matrix dstMatrix;
        float[] dstVals = new float[9];
        // get src chart translation matrix:
        srcMatrix = mSource.getViewPortHandler().getMatrixTouch();
        srcMatrix.getValues(srcVals);
        // apply X axis scaling and position to dst charts:
        for (Chart client : mTargets) {
            if (client.getVisibility() == View.VISIBLE) {
                dstMatrix = client.getViewPortHandler().getMatrixTouch();
                dstMatrix.getValues(dstVals);

                dstVals[Matrix.MSCALE_X] = srcVals[Matrix.MSCALE_X];
                dstVals[Matrix.MSKEW_X] = srcVals[Matrix.MSKEW_X];
                dstVals[Matrix.MTRANS_X] = srcVals[Matrix.MTRANS_X];
                dstVals[Matrix.MSKEW_Y] = srcVals[Matrix.MSKEW_Y];
                dstVals[Matrix.MSCALE_Y] = srcVals[Matrix.MSCALE_Y];
                dstVals[Matrix.MTRANS_Y] = srcVals[Matrix.MTRANS_Y];
                dstVals[Matrix.MPERSP_0] = srcVals[Matrix.MPERSP_0];
                dstVals[Matrix.MPERSP_1] = srcVals[Matrix.MPERSP_1];
                dstVals[Matrix.MPERSP_2] = srcVals[Matrix.MPERSP_2];

                dstMatrix.setValues(dstVals);
                client.getViewPortHandler().refresh(dstMatrix, client, true);
            }
        }
    }
}